<?php
namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Models\Exchange;
use Illuminate\Http\Request;

class ExchangeRateController extends Controller
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '汇率';



    public function index(Request $req)
    {
        $all = $req->all();
//        $per_page = isset($all['per_page']) ? $all['per_page'] :20;
        $per_page = $this->getPerPage($req);

        $qry = Exchange::query()
            ->orderByDesc('term');
        if (isset($all['term'])) {
            $qry->where('term', $all['term']);
        }
        $list = $qry->paginate($per_page);

        return $this->resSuccess('', $list);

    }


    // 新增
    public function store()
    {
        $row = request()->all();
        $qry = Exchange::query();
        $qry->where('origin', $row['origin'])
            ->where('currency',$row['currency'])
            ->where('term',$row['term']);

        $exist = $qry->first();
        if ($exist) {
            return $this->resError('汇率已经存在：'.$row['term'].'-'.$row['origin'].'-'.$row['currency']);
        }

        $obj = new Exchange($row);
        // 操作日志
//        $form = $this->form();

        if($obj->save()) {
//            self::saveLog($form, 'origin', $obj);
            return  $this->resSuccess();
        } else {
            return $this->resError();
        }
    }

    // 修改
    public function update($id)
    {
        $row = request()->all();
        $qry = Exchange::query();
        $qry->where('id','<>',$id)
            ->where('origin', $row['origin'])
            ->where('currency',$row['currency'])
            ->where('term',$row['term']);

        $exist = $qry->first();
        if ($exist) {
            return $this->resError('汇率已经存在：'.$row['term'].'-'.$row['origin'].'-'.$row['currency']);
        }
        $obj = Exchange::find($id);
//        $origin = $obj->toArray();
        $obj->fill($row);

        // 操作日志
//        $form = $this->form();
//        self::setOrigin($form, $obj);

        if($obj->save()) {
//            self::saveLog($form, 'origin', $obj);
            return  $this->resSuccess();
        } else {
            return  $this->resError();
        }


    }


    protected function grid()
    {
        $grid = new Grid(new Exchange());
//        $grid->actions(function ($actions) {
//            $actions->disableView();
////            $actions->disableDelete();
////            $actions->disableEdit();
//        });

//        request()->get('term');
//        $term = request()->get('term');

//        $grid->disableCreateButton();
//        $grid->model()->whereIn('currency',config('yt.currency'));
//        $grid->column('id', 'ID');
        $grid->model()->orderByDesc('term');
        $grid->column('term', '时间');
        $grid->column('version_date', '版本时间');
        $grid->column('origin', '原始币别');
        $grid->column('medium', '中介币别');
        $grid->column('medium_rate', '中介汇率');
//        $grid->column('currency', __('币种'));
        $grid->column('currency', __('最终币别'));
        $grid->column('value', '最终汇率');

//        $grid->column('value', __('交易佣金(成交费)'))->display(function ($value){
//            if ($this->usd) {
//                return '1USD = '.$value.$this->currency;
//            } else {
//                return "1$this->origin = ".$value.$this->currency;
//            }
//        });

//        $grid->column('', '说明')->display(function (){
//            return "1 $this->origin = ".$this->value . $this->currency;
//        });

        $grid->filter(function ($filter) {
            $filter->expand();
            $filter->disableIdFilter();
            $filter->column(1/4,function($filter) {
                $filter->equal('term', '时间（格式：2021-01）');
            });
        });

        $this->toolsButton(new ImportExchangeAction());
//        $grid->tools(function ($tools) {
//            $tools->append(new ImportExchangeAction());
//        });

        $this->disableButton(['view']);

        return $grid;
    }

//    use \App\Traits\OperationLog;







    public function detail($id)
    {
        $show = new Show(Exchange::find($id));
        $show->field('term','日期');
        $show->field('version_date', '版本日期');
        $show->field('origin','原始币别');
        $show->field('currency','最终币别');
        $show->field('value','汇率');
        $show->field('created_at','创建时间');
        $show->field('updated_at','更新时间');

        return $show;
    }


    public function form()
    {
        $month = date('Y-m');
        $form = new Form(new Exchange);
        $form->text('term','日期')->width('100px')->default($month)
            ->help('格式：2021-01')->required();
        $form->date('version_date', '版本日期')->required();
        $form->text('origin','原始币别')->required();
        $form->text('medium','中间币别')->required();
        $form->text('medium_rate','中间汇率')->required();
        $form->text('currency','最终币别')->required();
        $form->text('value','汇率')->required();
//        $id = @request()->route()->parameters()['exchange'];
//        $form->setAction('/admin/exchange/save/'.$id);


        return $form;
    }





}
